(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	function getlist(curpage,pagenum){
	   var leveling_phone=$("#leveling_phone").val();
	   var leveling_name=$("#leveling_name").val();
	   var status=$("#status").val()==0?"":$("#status").val();
	   var leveling_account=$("#leveling_account").val();
	   var startTime=$("#datepicker").val();
	   var endTime=$("#datepicker2").val();
	   $.ajax({
			// url:'http://web.icecn.net/leveling/getLevelingManager.json',
			url:httpOrigin + '/leveling/getLevelingManager.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				leveling_phone:leveling_phone,
				leveling_name:leveling_name,
				status:status,
				leveling_account:leveling_account,
				startTime:startTime,
				endTime:endTime
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
						for(var i=0;i<dataset.length;i++){
							switch(dataset[i].status){
							 case 1 :{
								dataset[i].status="正常" ;
								break;
							 }
							 case 2 :{
								dataset[i].status="禁止登录" ;
								break;
							 }
							 case 3 :{
								dataset[i].status="禁言" ;
								break;
							 }
							 case 4 :{
								dataset[i].status="禁止Ip登录" ;
								break;
							 }
							 case 5 :{
								dataset[i].status="列表不可见" ;
								break;
							 }
							 case 6 :{
								dataset[i].status="禁止接单" ;
								break;
							 }
						  }	   
					   }
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
					   $("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>");	
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
				curpage=1
			  	getlist(curpage,pagenum)
			})
			//全选
			$(".content").on("click", '.allcheck', function() {
				if ($(".allcheck").attr("checked")) { 
				    $(".allcheck").attr("checked", false)
					$("input[name=items]").each(function() {  
						$(this).attr("checked", false);  
					});  
				} else {  
				    $(".allcheck").attr("checked", true)
					$("input[name=items]").each(function() {  
						$(this).attr("checked", true);  
					});  
				}  
			})
			
			//解绑资格
			$(".btnDisqualified").click(function(){
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/leveling/deleteLevelings.json',
					type:'POST',
					async:true,
					data:{
						ids:id,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-Disqualified").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			
			//解绑邮箱
			$(".btnemail").click(function(){
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/leveling/updateLevelingEmail.json',
					type:'POST',
					async:true,
					data:{
						ids:id,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-email").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			
			//解绑手机
			$(".btnphone").click(function(){
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/leveling/updateLevelingPhone.json',
					type:'POST',
					async:true,
					data:{
						ids:id,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-phone").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			
			//修改状态
			$(".content-wrapper").on("click", '.status', function() {
				$("#modal-status .modal-body").attr("data-status",$(this).attr("data-status"))
				$("#modal-status .modal-body").html("确认"+$(this).html()+"?")
			})
			//确认修改状态
			$(".btnstatus").click(function(){
			   var status=$("#modal-status .modal-body").attr("data-status");
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/leveling/setUserStatus.json',
					type:'POST',
					async:true,
					data:{
						ids:id,
						status:status
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-status").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			
			//游戏置顶
			$(".content-wrapper").on("click", '.istop', function() {
				var id=$(this).attr("data-id")
				$("#modal-istop .modal-body").attr("data-id",id)
				$.ajax({
					// url:'http://web.icecn.net/leveling/getLemInfo.json',
					url:httpOrigin + '/leveling/getLemInfo.json',

					type:'POST',
					async:true,
					data:{
						id:id,
					},
			        xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							var list=data.t;
							var a="";
							for(var i=0;i<list.length;i++){
								var leveling_gametype=list[i].leveling_gametype==1?'王者荣耀':'绝地求生';
								a+="<input type='radio' name='RadioGroup1' value='"+list[i].id+"'>"+leveling_gametype+"<br>";
							}
							a+="<input type='radio' name='RadioGroup1' value='0'>不置顶<br>";
							$(".lems").html(a);
						}else{
						    alert(data.msg)	
						}
					}
			   });
			})
			//确认游戏置顶
			$(".btnistop").click(function(){
			   var id=$("#modal-istop input[name=RadioGroup1]:checked").val();
			   var le_id=$("#modal-istop .modal-body").attr("data-id")
			   if(id==""||id==null){alert("请选择一项");return}
			   $.ajax({
					url:'http://web.icecn.net/leveling/updateLevelingManager.json',
					type:'POST',
					async:true,
					data:{
						ids:id,
						le_id:le_id
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-istop").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();